from typing import Dict, List, Optional
from datetime import datetime
from Course_Management_and_Enrollment.course_task_classes import Course
from User_Management.user_classes import Student

class ProgressTracker:
    def __init__(self):
        # 进度记录结构: {student_id: {course_id: {module_id: completion_status}, ...}, ...}
        self.progress_records = {}
        self.progress = {}

    def update_progress(self, student_id: str, course_id: str, module_id: str, is_completed: bool) -> None:
        """更新学生课程模块的进度"""
        if student_id not in self.progress_records:
            self.progress_records[student_id] = {}
        if course_id not in self.progress_records[student_id]:
            self.progress_records[student_id][course_id] = {}
        
        self.progress_records[student_id][course_id][module_id] = is_completed
        
    def get_progress_percentage(self, student_id: str, course_id: str) -> float:
        """获取学生课程的进度百分比"""
        if student_id not in self.progress_records or course_id not in self.progress_records[student_id]:
            return 0.0
        
        modules = self.progress_records[student_id][course_id]
        if not modules:
            return 0.0
        
        completed_count = sum(1 for completed in modules.values() if completed)
        return (completed_count / len(modules)) * 100
        
    def generate_progress_report(self, student: Student) -> str:
        """生成学生进度报告"""
        report = f"# Student Progress Report\n"
        report += f"Student Name: {student.username}\n"
        report += f"Student ID: {student.student_id}\n"
        report += f"Generation time: {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}\n"
        report += f"==============================\n\n"
        
        # 遍历学生已选课程
        for course in student.enrolled_courses:
            progress_percentage = self.get_progress_percentage(student.student_id, course.course_id)
            report += f"📚 Course: {course.course_name}\n"
            report += f"   Progress: {progress_percentage:.1f}%\n"
            report += f"   ------------------------------\n\n"
        
        return report
        
    def _init_empty_progress(self, student_id: str, course_id: str) -> None:
        """初始化空进度记录"""
        if student_id not in self.progress_records:
            self.progress_records[student_id] = {}
        if course_id not in self.progress_records[student_id]:
            self.progress_records[student_id][course_id] = {}
            
    def load_progress_data(self, progress_data: dict) -> None:
        """加载进度数据"""
        self.progress_records = progress_data
